<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-24
 * Time: 9:46
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\SkillType as SkillTypeModel;
use think\Db;

class SkillType extends AdminBase
{
    /**
     * 技能列表
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($type = '', $page = 1)
    {
        $map = [];
        if ($type){
            if ($type != '请选择'){
                $map= ['st_type' => $type];
            }
        }
        $list = SkillTypeModel::skillStypeList($map, $page);

        return $this->fetch('index', ['list' => $list, 'type' => $type]);
    }

    /**
     * 技能页面
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 添加技能
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();

            if (array_key_exists('photo', $data)) {
                $data['st_photo_just'] = $data['photo'][0];
                $data['st_photo_back'] = $data['photo'][1];
            }
            unset($data['editorValue']);
            unset($data['photo']);
            $status = SkillTypeModel::skillSave($data);
            if ($status) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑技能
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $detail = SkillTypeModel::detail($id);
        return $this->fetch('edit', ['detail' => $detail]);
    }

    /**
     * 更新技能
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $data['st_update_time'] = time();
            $status = SkillTypeModel::updateType($data);
            if ($status) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 禁用启用
     * @param $id
     */
    public function editstatus($id, $status)
    {
        if ($status == 1) {
            $status = 2;
        } else if ($status == 2) {
            $status = 1;
        } else {
            $this->error('修改失败');
        }
        $status = SkillTypeModel::editStatus($id, $status);
        if ($status) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    /**
     * 删除技能
     * @param int   $id
     */
    public function delete($id)
    {
        if (SkillTypeModel::del($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}